package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"
)

func main() {
	var h = flag.String("h", "", "header, 显示以本参数开头的文件名")
	var t = flag.String("t", "", "tail, 显示以本参数结尾的文件名")
	var c = flag.String("c", "", "contain, 显示包含本参数的文件名")
	flag.Parse()

	d, err := os.Open(".")
	if err != nil {
		panic(err)
	}
	defer d.Close()
	list1, err := d.Readdir(-1)
	if err != nil {
		panic(err)
	}
	list2 := []os.FileInfo{}
	for _, f := range list1 {
		pick := true
		if *h != "" {
			if strings.HasPrefix(f.Name(), *h) == false {
				pick = false
			}
		}
		if *t != "" {
			if strings.HasSuffix(f.Name(), *t) == false {
				pick = false
			}
		}
		if *c != "" {
			if strings.Contains(f.Name(), *c) == false {
				pick = false
			}
		}
		if pick {
			list2 = append(list2, f)
		}
	}
	list1 = list2
	for n, f := range list1 {
		if f.IsDir() {
			continue
		}
		println(n, ":", f.Name())
	}
	println("选择要删除的文件编号(用‘,’分隔):")
	var s string
	fmt.Scanln(&s)
	nums := strings.Split(s, ",")
	for _, num := range nums {
		n, err := strconv.Atoi(num)
		if err != nil {
			println("Error:", num)
			continue
		}

		err = os.Remove(list1[n].Name())
		res := "[成功]"
		if err != nil {
			res = "[失败]"
			log.Println(err.Error())
		}

		println("删除:", num, list1[n].Name(), res)
	}

}
